-- UIDungeonSailor
-- Created by panyl Apr/29/2015
-- 水手残骸界面

UIDungeonSailor = class("UIDungeonSailor", function()
    return cc.Layer:create();
end);

function UIDungeonSailor.create(pos)
    return UIDungeonSailor.new(pos);
end

function UIDungeonSailor:ctor(pos)
    -- 初始化
    self:setName("UIDungeonSailor");
    local node = cc.CSLoader:createNode("layout/dungeon/DungeonSailor.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.pos = pos;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIDungeonSailor:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIDungeonSailor:registerEventCallback()
    -- -- 关注埋葬水手残骸事件
    -- EventMgr.register("UIDungeonSailor", event.BURY_SAILOR, function()
    --     alert(getLocStr("bury_sailor_tip"));

    --     -- 关闭界面
    --     UIMgr.getCurrentScene():removeFormByName("UIDungeonSailor");
    -- end)

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIDungeonSailor 界面析构清理");

            EventMgr.removeAll("UIDungeonSailor");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIDungeonSailor:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIDungeonSailor");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 翻找
    local rummageBtn = findChildByName(node, "CT/btn_rummage");
    local pos = self.pos;
    local function okClickRummage(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            DungeonActionM.go("do_building_bonus", pos);

            -- 关闭界面
            UIMgr.getCurrentScene():removeFormByName("UIDungeonSailor");
        end
    end
    rummageBtn:addTouchEventListener(okClickRummage);
    TextStyleM.setTextStyle(rummageBtn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    rummageBtn:setTitleText(string.format(getLocStr("rummage_button"), rummageBtn.wager));

    -- 埋葬水手残骸
    local buryBtn = findChildByName(node, "CT/btn_bury");
    local function okClickBury(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            -- 埋葬水手残骸，幽灵船等级+1
            DungeonActionM.go("bury_sailor", pos);

            -- 提示
            alert(getLocStr("bury_sailor_tip"));

            -- 关闭界面
            UIMgr.getCurrentScene():removeFormByName("UIDungeonSailor");
        end
    end
    buryBtn:addTouchEventListener(okClickBury);
    TextStyleM.setTextStyle(buryBtn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    buryBtn:setTitleText(string.format(getLocStr("bury_button"), buryBtn.wager));
end

-- 重绘
function UIDungeonSailor:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");

    local gridData = DungeonM.getGridByPos(self.pos);
    local info = BuildingBonusM.queryBuildingView(gridData.class);

    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    applyString(descLabel, info["dialog"]);
    titleLabel:setString(info["title"]);
    iconImg:loadTexture(getLevelItemIconPath(info["icon"]));

    iconImg:setScale(1.5);
    TextStyleM.setArtWordTitleStyle(titleLabel);
end

